home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / builtins / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-29  |  21.0 KB  |  764 lines

  1. /* Getopt for GNU.
  2.    NOTE: getopt is now part of the C library, so if you don't know what
  3.    "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
  4.    before changing it!
  5.  
  6.    Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
  7.        Free Software Foundation, Inc.
  8.  
  9.    This program is free software; you can redistribute it and/or modify it
  10.    under the terms of the GNU General Public License as published by the
  11.    Free Software Foundation; either version 2, or (at your option) any
  12.    later version.
  13.  
  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.
  18.  
  19.    You should have received a copy of the GNU General Public License
  20.    along with this program; if not, write to the Free Software
  21.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. /* NOTE!!!  AIX requires this to be the first thing in the file.
  24.    Do not put ANYTHING before it!  */
  25. #if !defined (__GNUC__) && defined (_AIX) && !defined (IBMESA)
  26.  #pragma alloca
  27. #endif
  28.  
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32.  
  33. #ifdef __GNUC__
  34. #define alloca __builtin_alloca
  35. #else /* not __GNUC__ */
  36. #if defined (HAVE_ALLOCA_H) || (defined(sparc) && (defined(sun) || (!defined(USG) && !defined(SVR4) && !defined(__svr4__))))
  37. #if defined (IBMESA)
  38. #include <malloc.h>
  39. #else
  40. #include <alloca.h>
  41. #endif /* !IBMESA */
  42. #else
  43. #ifndef _AIX
  44. char *alloca ();
  45. #endif
  46. #endif /* alloca.h */
  47. #endif /* not __GNUC__ */
  48.  
  49. #if !__STDC__ && !defined(const) && IN_GCC
  50. #define const
  51. #endif
  52.  
  53. /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.  */
  54. #ifndef _NO_PROTO
  55. #define _NO_PROTO
  56. #endif
  57.  
  58. #include <stdio.h>
  59.  
  60. /* Comment out all this code if we are using the GNU C Library, and are not
  61.    actually compiling the library itself.  This code is part of the GNU C
  62.    Library, but also included in many other GNU distributions.  Compiling
  63.    and linking in this code is a waste when using the GNU C library
  64.    (especially if it is a shared library).  Rather than having every GNU
  65.    program understand `configure --with-gnu-libc' and omit the object files,
  66.    it is simpler to just do this in the source for each such file.  */
  67.  
  68. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  69.  
  70.  
  71. /* This needs to come after some library #include
  72.    to get __GNU_LIBRARY__ defined.  */
  73. #ifdef    __GNU_LIBRARY__
  74. #undef    alloca
  75. /* Don't include stdlib.h for non-GNU C libraries because some of them
  76.    contain conflicting prototypes for getopt.  */
  77. #include <stdlib.h>
  78. #else    /* Not GNU C library.  */
  79. #define    __alloca    alloca
  80. #endif    /* GNU C library.  */
  81.  
  82. #if !defined (__STDC__) && !defined (const)
  83. #  define const
  84. #endif
  85.  
  86. /* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a
  87.    long-named option.  Because this is not POSIX.2 compliant, it is
  88.    being phased out.  */
  89. /* #define GETOPT_COMPAT */
  90.  
  91. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  92.    but it behaves differently for the user, since it allows the user
  93.    to intersperse the options with the other arguments.
  94.  
  95.    As `getopt' works, it permutes the elements of ARGV so that,
  96.    when it is done, all the options precede everything else.  Thus
  97.    all application programs are extended to handle flexible argument order.
  98.  
  99.    Setting the environment variable POSIXLY_CORRECT disables permutation.
  100.    Then the behavior is completely standard.
  101.  
  102.    GNU application programs can use a third alternative mode in which
  103.    they can distinguish the relative order of options and other arguments.  */
  104.  
  105. #include "getopt.h"
  106.  
  107. /* For communication from `getopt' to the caller.
  108.    When `getopt' finds an option that takes an argument,
  109.    the argument value is returned here.
  110.    Also, when `ordering' is RETURN_IN_ORDER,
  111.    each non-option ARGV-element is returned here.  */
  112.  
  113. char *optarg = 0;
  114.  
  115. /* Index in ARGV of the next element to be scanned.
  116.    This is used for communication to and from the caller
  117.    and for communication between successive calls to `getopt'.
  118.  
  119.    On entry to `getopt', zero means this is the first call; initialize.
  120.  
  121.    When `getopt' returns EOF, this is the index of the first of the
  122.    non-option elements that the caller should itself scan.
  123.  
  124.    Otherwise, `optind' communicates from one call to the next
  125.    how much of ARGV has been scanned so far.  */
  126.  
  127. /* XXX 1003.2 says this must be 1 before any call.  */
  128. int optind = 0;
  129.  
  130. /* The next char to be scanned in the option-element
  131.    in which the last option character we returned was found.
  132.    This allows us to pick up the scan where we left off.
  133.  
  134.    If this is zero, or a null string, it means resume the scan
  135.    by advancing to the next ARGV-element.  */
  136.  
  137. static char *nextchar;
  138.  
  139. /* Callers store zero here to inhibit the error message
  140.    for unrecognized options.  */
  141.  
  142. int opterr = 1;
  143.  
  144. /* Set to an option character which was unrecognized.
  145.    This must be initialized on some systems to avoid linking in the
  146.    system's own getopt implementation.  */
  147.  
  148. int optopt = '?';
  149.  
  150. /* Describe how to deal with options that follow non-option ARGV-elements.
  151.  
  152.    If the caller did not specify anything,
  153.    the default is REQUIRE_ORDER if the environment variable
  154.    POSIXLY_CORRECT is defined, PERMUTE otherwise.
  155.  
  156.    REQUIRE_ORDER means don't recognize them as options;
  157.    stop option processing when the first non-option is seen.
  158.    This is what Unix does.
  159.    This mode of operation is selected by either setting the environment
  160.    variable POSIXLY_CORRECT, or using `+' as the first character
  161.    of the list of option characters.
  162.  
  163.    PERMUTE is the default.  We permute the contents of ARGV as we scan,
  164.    so that eventually all the non-options are at the end.  This allows options
  165.    to be given in any order, even with programs that were not written to
  166.    expect this.
  167.  
  168.    RETURN_IN_ORDER is an option available to programs that were written
  169.    to expect options and other ARGV-elements in any order and that care about
  170.    the ordering of the two.  We describe each non-option ARGV-element
  171.    as if it were the argument of an option with character code 1.
  172.    Using `-' as the first character of the list of option characters
  173.    selects this mode of operation.
  174.  
  175.    The special argument `--' forces an end of option-scanning regardless
  176.    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
  177.    `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
  178.  
  179. static enum
  180. {
  181.   REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
  182. } ordering, default_ordering = PERMUTE;
  183.  
  184. #ifdef    __GNU_LIBRARY__
  185. /* We want to avoid inclusion of string.h with non-GNU libraries
  186.    because there are many ways it can cause trouble.
  187.    On some systems, it contains special magic macros that don't work
  188.    in GCC.  */
  189. #include <string.h>
  190. #define    my_index    strchr
  191. #define    my_bcopy(src, dst, n)    memcpy ((dst), (src), (n))
  192. #else
  193.  
  194. /* Avoid depending on library functions or files
  195.    whose names are inconsistent.  */
  196.  
  197. char *getenv ();
  198.  
  199. static char *
  200. my_index (str, chr)
  201.      const char *str;
  202.      int chr;
  203. {
  204.   while (*str)
  205.     {
  206.       if (*str == chr)
  207.     return (char *) str;
  208.       str++;
  209.     }
  210.   return 0;
  211. }
  212.  
  213. static void
  214. my_bcopy (from, to, size)
  215.      const char *from;
  216.      char *to;
  217.      int size;
  218. {
  219.   int i;
  220.   for (i = 0; i < size; i++)
  221.     to[i] = from[i];
  222. }
  223. #endif                /* GNU C library.  */
  224.  
  225. /* Handle permutation of arguments.  */
  226.  
  227. /* Describe the part of ARGV that contains non-options that have
  228.    been skipped.  `first_nonopt' is the index in ARGV of the first of them;
  229.    `last_nonopt' is the index after the last of them.  */
  230.  
  231. static int first_nonopt;
  232. static int last_nonopt;
  233.  
  234. /* Exchange two adjacent subsequences of ARGV.
  235.    One subsequence is elements [first_nonopt,last_nonopt)
  236.    which contains all the non-options that have been skipped so far.
  237.    The other is elements [last_nonopt,optind), which contains all
  238.    the options processed since those non-options were skipped.
  239.  
  240.    `first_nonopt' and `last_nonopt' are relocated so that they describe
  241.    the new indices of the non-options in ARGV after they are moved.  */
  242.  
  243. static void
  244. exchange (argv)
  245.      char **argv;
  246. {
  247.   int nonopts_size = (last_nonopt - first_nonopt) * sizeof (char *);
  248.   char **temp = (char **) __alloca (nonopts_size);
  249.  
  250.   /* Interchange the two blocks of data in ARGV.  */
  251.  
  252.   my_bcopy ((char *) &argv[first_nonopt], (char *) temp, nonopts_size);
  253.   my_bcopy ((char *) &argv[last_nonopt], (char *) &argv[first_nonopt],
  254.         (optind - last_nonopt) * sizeof (char *));
  255.   my_bcopy ((char *) temp,
  256.         (char *) &argv[first_nonopt + optind - last_nonopt],
  257.         nonopts_size);
  258.  
  259.   /* Update records for the slots the non-options now occupy.  */
  260.  
  261.   first_nonopt += (optind - last_nonopt);
  262.   last_nonopt = optind;
  263. }
  264.  
  265. /* Scan elements of ARGV (whose length is ARGC) for option characters
  266.    given in OPTSTRING.
  267.  
  268.    If an element of ARGV starts with '-', and is not exactly "-" or "--",
  269.    then it is an option element.  The characters of this element
  270.    (aside from the initial '-') are option characters.  If `getopt'
  271.    is called repeatedly, it returns successively each of the option characters
  272.    from each of the option elements.
  273.  
  274.    If `getopt' finds another option character, it returns that character,
  275.    updating `optind' and `nextchar' so that the next call to `getopt' can
  276.    resume the scan with the following option character or ARGV-element.
  277.  
  278.    If there are no more option characters, `getopt' returns `EOF'.
  279.    Then `optind' is the index in ARGV of the first ARGV-element
  280.    that is not an option.  (The ARGV-elements have been permuted
  281.    so that those that are not options now come last.)
  282.  
  283.    OPTSTRING is a string containing the legitimate option characters.
  284.    If an option character is seen that is not listed in OPTSTRING,
  285.    return '?' after printing an error message.  If you set `opterr' to
  286.    zero, the error message is suppressed but we still return '?'.
  287.  
  288.    If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  289.    so the following text in the same ARGV-element, or the text of the following
  290.    ARGV-element, is returned in `optarg'.  Two colons mean an option that
  291.    wants an optional arg; if there is text in the current ARGV-element,
  292.    it is returned in `optarg', otherwise `optarg' is set to zero.
  293.  
  294.    If OPTSTRING starts with `-' or `+', it requests different methods of
  295.    handling the non-option ARGV-elements.
  296.    See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
  297.  
  298.    Long-named options begin with `--' instead of `-'.
  299.    Their names may be abbreviated as long as the abbreviation is unique
  300.    or is an exact match for some defined option.  If they have an
  301.    argument, it follows the option name in the same ARGV-element, separated
  302.    from the option name by a `=', or else the in next ARGV-element.
  303.    When `getopt' finds a long-named option, it returns 0 if that option's
  304.    `flag' field is nonzero, the value of the option's `val' field
  305.    if the `flag' field is zero.
  306.  
  307.    The elements of ARGV aren't really const, because we permute them.
  308.    But we pretend they're const in the prototype to be compatible
  309.    with other systems.
  310.  
  311.    LONGOPTS is a vector of `struct option' terminated by an
  312.    element containing a name which is zero.
  313.  
  314.    LONGIND returns the index in LONGOPT of the long-named option found.
  315.    It is only valid when a long-named option has been found by the most
  316.    recent call.
  317.  
  318.    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
  319.    long-named options.  */
  320.  
  321. /* Call this function with an argument of 1 to set the default option
  322.    ordering to that required by Posix.  The normal default is PERMUTE. */
  323. void
  324. getopt_set_posix_option_order (on_or_off)
  325.      int on_or_off;
  326. {
  327.   if (on_or_off == 1)
  328.     default_ordering = REQUIRE_ORDER;
  329.   else
  330.     default_ordering = PERMUTE;
  331. }
  332.     
  333. int
  334. _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
  335.      int argc;
  336.      char *const *argv;
  337.      const char *optstring;
  338.      const struct option *longopts;
  339.      int *longind;
  340.      int long_only;
  341. {
  342.   int option_index;
  343.  
  344.   optarg = 0;
  345.  
  346.   if (optind > argc || optind < 0)
  347.     {
  348.       optind = argc;
  349.       return (EOF);
  350.     }
  351.  
  352.   /* Initialize the internal data when the first call is made.
  353.      Start processing options with ARGV-element 1 (since ARGV-element 0
  354.      is the program name); the sequence of previously skipped
  355.      non-option ARGV-elements is empty.  */
  356.  
  357.   if (optind == 0)
  358.     {
  359.       first_nonopt = last_nonopt = optind = 1;
  360.  
  361.       nextchar = NULL;
  362.  
  363.       /* Determine how to handle the ordering of options and nonoptions.  */
  364.  
  365.       if (optstring[0] == '-')
  366.     {
  367.       ordering = RETURN_IN_ORDER;
  368.       ++optstring;
  369.     }
  370.       else if (optstring[0] == '+')
  371.     {
  372.       ordering = REQUIRE_ORDER;
  373.       ++optstring;
  374.     }
  375.       else if (getenv ("POSIXLY_CORRECT") != NULL)
  376.     ordering = REQUIRE_ORDER;
  377.       else
  378.     ordering = default_ordering;
  379.     }
  380.  
  381.   if (nextchar == NULL || *nextchar == '\0')
  382.     {
  383.       if (ordering == PERMUTE)
  384.     {
  385.       /* If we have just processed some options following some non-options,
  386.          exchange them so that the options come first.  */
  387.  
  388.       if (first_nonopt != last_nonopt && last_nonopt != optind)
  389.         exchange ((char **) argv);
  390.       else if (last_nonopt != optind)
  391.         first_nonopt = optind;
  392.  
  393.       /* Now skip any additional non-options
  394.          and extend the range of non-options previously skipped.  */
  395.  
  396.       while (optind < argc
  397.          && (argv[optind][0] != '-' || argv[optind][1] == '\0')
  398. #ifdef GETOPT_COMPAT
  399.          && (longopts == NULL
  400.              || argv[optind][0] != '+' || argv[optind][1] == '\0')
  401. #endif                /* GETOPT_COMPAT */
  402.          )
  403.         optind++;
  404.       last_nonopt = optind;
  405.     }
  406.  
  407.       /* Special ARGV-element `--' means premature end of options.
  408.      Skip it like a null option,
  409.      then exchange with previous non-options as if it were an option,
  410.      then skip everything else like a non-option.  */
  411.  
  412.       if (optind != argc && !strcmp (argv[optind], "--"))
  413.     {
  414.       optind++;
  415.  
  416.       if (first_nonopt != last_nonopt && last_nonopt != optind)
  417.         exchange ((char **) argv);
  418.       else if (first_nonopt == last_nonopt)
  419.         first_nonopt = optind;
  420.       last_nonopt = argc;
  421.  
  422.       optind = argc;
  423.     }
  424.  
  425.       /* If we have done all the ARGV-elements, stop the scan
  426.      and back over any non-options that we skipped and permuted.  */
  427.  
  428.       if (optind == argc)
  429.     {
  430.       /* Set the next-arg-index to point at the non-options
  431.          that we previously skipped, so the caller will digest them.  */
  432.       if (first_nonopt != last_nonopt)
  433.         optind = first_nonopt;
  434.       return EOF;
  435.     }
  436.  
  437.       /* If we have come to a non-option and did not permute it,
  438.      either stop the scan or describe it to the caller and pass it by.  */
  439.  
  440.       if ((argv[optind][0] != '-' || argv[optind][1] == '\0')
  441. #ifdef GETOPT_COMPAT
  442.       && (longopts == NULL
  443.           || argv[optind][0] != '+' || argv[optind][1] == '\0')
  444. #endif                /* GETOPT_COMPAT */
  445.       )
  446.     {
  447.       if (ordering == REQUIRE_ORDER)
  448.         return EOF;
  449.       optarg = argv[optind++];
  450.       return 1;
  451.     }
  452.  
  453.       /* We have found another option-ARGV-element.
  454.      Start decoding its characters.  */
  455.  
  456.       nextchar = (argv[optind] + 1
  457.           + (longopts != NULL && argv[optind][1] == '-'));
  458.     }
  459.  
  460.   if (longopts != NULL
  461.       && ((argv[optind][0] == '-'
  462.        && (argv[optind][1] == '-' || long_only))
  463. #ifdef GETOPT_COMPAT
  464.       || argv[optind][0] == '+'
  465. #endif                /* GETOPT_COMPAT */
  466.       ))
  467.     {
  468.       const struct option *p;
  469.       char *s = nextchar;
  470.       int exact = 0;
  471.       int ambig = 0;
  472.       const struct option *pfound = NULL;
  473.       int indfound;
  474.  
  475.       while (*s && *s != '=')
  476.     s++;
  477.  
  478.       /* Test all options for either exact match or abbreviated matches.  */
  479.       for (p = longopts, option_index = 0; p->name;
  480.        p++, option_index++)
  481.     if (!strncmp (p->name, nextchar, s - nextchar))
  482.       {
  483.         if (s - nextchar == strlen (p->name))
  484.           {
  485.         /* Exact match found.  */
  486.         pfound = p;
  487.         indfound = option_index;
  488.         exact = 1;
  489.         break;
  490.           }
  491.         else if (pfound == NULL)
  492.           {
  493.         /* First nonexact match found.  */
  494.         pfound = p;
  495.         indfound = option_index;
  496.           }
  497.         else
  498.           /* Second nonexact match found.  */
  499.           ambig = 1;
  500.       }
  501.  
  502.       if (ambig && !exact)
  503.     {
  504.       if (opterr)
  505.         fprintf (stderr, "%s: option `%s' is ambiguous\n",
  506.              argv[0], argv[optind]);
  507.       nextchar = "";
  508.       optind++;
  509.       return '?';
  510.     }
  511.  
  512.       if (pfound != NULL)
  513.     {
  514.       option_index = indfound;
  515.       optind++;
  516.       if (*s)
  517.         {
  518.           /* Don't test has_arg with >, because some C compilers don't
  519.          allow it to be used on enums.  */
  520.           if (pfound->has_arg)
  521.         optarg = s + 1;
  522.           else
  523.         {
  524.           if (opterr)
  525.             {
  526.               if (argv[optind - 1][1] == '-')
  527.             /* --option */
  528.             fprintf (stderr,
  529.                  "%s: option `--%s' doesn't allow an argument\n",
  530.                  argv[0], pfound->name);
  531.               else
  532.             /* +option or -option */
  533.             fprintf (stderr,
  534.                  "%s: option `%c%s' doesn't allow an argument\n",
  535.                  argv[0], argv[optind - 1][0], pfound->name);
  536.             }
  537.  
  538.           nextchar = "";
  539.           return '?';
  540.         }
  541.         }
  542.       else if (pfound->has_arg == 1)
  543.         {
  544.           if (optind < argc)
  545.         optarg = argv[optind++];
  546.           else
  547.         {
  548.           if (opterr)
  549.             fprintf (stderr, "%s: option `%s' requires an argument\n",
  550.                  argv[0], argv[optind - 1]);
  551.           nextchar = "";
  552.           return optstring[0] == ':' ? ':' : '?';
  553.         }
  554.         }
  555.       nextchar = "";
  556.       if (longind != NULL)
  557.         *longind = option_index;
  558.       if (pfound->flag)
  559.         {
  560.           *(pfound->flag) = pfound->val;
  561.           return 0;
  562.         }
  563.       return pfound->val;
  564.     }
  565.       /* Can't find it as a long option.  If this is not getopt_long_only,
  566.      or the option starts with '--' or is not a valid short
  567.      option, then it's an error.
  568.      Otherwise interpret it as a short option.  */
  569.       if (!long_only || argv[optind][1] == '-'
  570. #ifdef GETOPT_COMPAT
  571.       || argv[optind][0] == '+'
  572. #endif                /* GETOPT_COMPAT */
  573.       || my_index (optstring, *nextchar) == NULL)
  574.     {
  575.       if (opterr)
  576.         {
  577.           if (argv[optind][1] == '-')
  578.         /* --option */
  579.         fprintf (stderr, "%s: unrecognized option `--%s'\n",
  580.              argv[0], nextchar);
  581.           else
  582.         /* +option or -option */
  583.         fprintf (stderr, "%s: unrecognized option `%c%s'\n",
  584.              argv[0], argv[optind][0], nextchar);
  585.         }
  586.       nextchar = (char *) "";
  587.       optind++;
  588.       return '?';
  589.     }
  590.     }
  591.  
  592.   /* Look at and handle the next option-character.  */
  593.  
  594.   {
  595.     char c = *nextchar++;
  596.     char *temp = my_index (optstring, c);
  597.  
  598.     /* Increment `optind' when we start to process its last character.  */
  599.     if (nextchar == NULL || *nextchar == '\0')
  600.       {
  601.         ++optind;
  602.         nextchar = (char *)NULL;
  603.       }
  604.  
  605.     optopt = c;
  606.  
  607.     if (temp == NULL || c == ':')
  608.       {
  609.     if (opterr)
  610.       {
  611. #if 0
  612.         if (c < 040 || c >= 0177)
  613.           fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
  614.                argv[0], c);
  615.         else
  616.           fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
  617. #else
  618.         /* 1003.2 specifies the format of this message.  */
  619.         fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
  620. #endif
  621.       }
  622.     optopt = c;
  623.     return '?';
  624.       }
  625.     if (temp[1] == ':')
  626.       {
  627.     if (temp[2] == ':')
  628.       {
  629.         /* This is an option that accepts an argument optionally.  */
  630.         if (nextchar && *nextchar != '\0')
  631.           {
  632.         optarg = nextchar;
  633.         optind++;
  634.           }
  635.         else
  636.           optarg = 0;
  637.         nextchar = (char *)NULL;
  638.       }
  639.     else
  640.       {
  641.         /* This is an option that requires an argument.  */
  642.         if (nextchar && *nextchar != '\0')
  643.           {
  644.         optarg = nextchar;
  645.         /* If we end this ARGV-element by taking the rest as an arg,
  646.            we must advance to the next element now.  */
  647.         optind++;
  648.           }
  649.         else if (optind == argc)
  650.           {
  651.         if (opterr)
  652.           {
  653. #if 0
  654.             fprintf (stderr, "%s: option `-%c' requires an argument\n",
  655.                  argv[0], c);
  656. #else
  657.             /* 1003.2 specifies the format of this message.  */
  658.             fprintf (stderr, "%s: option requires an argument -- %c\n",
  659.                  argv[0], c);
  660. #endif
  661.           }
  662.         optopt = c;
  663.         if (optstring[0] == ':')
  664.           c = ':';
  665.         else
  666.           c = '?';
  667.           }
  668.         else
  669.           /* We already incremented `optind' once;
  670.          increment it again when taking next ARGV-elt as argument.  */
  671.           optarg = argv[optind++];
  672.         nextchar = NULL;
  673.       }
  674.       }
  675.     return c;
  676.   }
  677. }
  678.  
  679. int
  680. getopt (argc, argv, optstring)
  681.      int argc;
  682.      char *const *argv;
  683.      const char *optstring;
  684. {
  685.   return _getopt_internal (argc, argv, optstring,
  686.                (const struct option *) 0,
  687.                (int *) 0,
  688.                0);
  689. }
  690.  
  691. #endif    /* _LIBC or not __GNU_LIBRARY__.  */
  692.  
  693. #ifdef TEST
  694.  
  695. /* Compile with -DTEST to make an executable for use in testing
  696.    the above definition of `getopt'.  */
  697.  
  698. int
  699. main (argc, argv)
  700.      int argc;
  701.      char **argv;
  702. {
  703.   int c;
  704.   int digit_optind = 0;
  705.  
  706.   while (1)
  707.     {
  708.       int this_option_optind = optind ? optind : 1;
  709.  
  710.       c = getopt (argc, argv, "abc:d:0123456789");
  711.       if (c == EOF)
  712.     break;
  713.  
  714.       switch (c)
  715.     {
  716.     case '0':
  717.     case '1':
  718.     case '2':
  719.     case '3':
  720.     case '4':
  721.     case '5':
  722.     case '6':
  723.     case '7':
  724.     case '8':
  725.     case '9':
  726.       if (digit_optind != 0 && digit_optind != this_option_optind)
  727.         printf ("digits occur in two different argv-elements.\n");
  728.       digit_optind = this_option_optind;
  729.       printf ("option %c\n", c);
  730.       break;
  731.  
  732.     case 'a':
  733.       printf ("option a\n");
  734.       break;
  735.  
  736.     case 'b':
  737.       printf ("option b\n");
  738.       break;
  739.  
  740.     case 'c':
  741.       printf ("option c with value `%s'\n", optarg);
  742.       break;
  743.  
  744.     case '?':
  745.       break;
  746.  
  747.     default:
  748.       printf ("?? getopt returned character code 0%o ??\n", c);
  749.     }
  750.     }
  751.  
  752.   if (optind < argc)
  753.     {
  754.       printf ("non-option ARGV-elements: ");
  755.       while (optind < argc)
  756.     printf ("%s ", argv[optind++]);
  757.       printf ("\n");
  758.     }
  759.  
  760.   exit (0);
  761. }
  762.  
  763. #endif /* TEST */
  764.